import org.eclipse.swt.SWT;
import java.util.ArrayList;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import java.sql.*;
import org.eclipse.swt.events.*;


public class addProductToPlace {
	Table table;
	TableColumn col1;
	TableColumn col2;
	TableItem[] selection;
	Shell parentShell;
	public addProductToPlace(final Shell parentShell,final ArrayList<TableItem> selectionP)
	{
		this.parentShell=parentShell;
		//selection=selectionP;
	
		final Shell shell = new Shell(parentShell);
		shell.setText("wybierz produkt");
		shell.setSize(400,400);
		table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION );
		
		
		FillLayout layout = new FillLayout();
		shell.setLayout(layout);
		Label szukajID = new Label(shell, SWT.CENTER);
		szukajID.setText("szukaj po id");
		Text idText = new Text(shell, SWT.SINGLE);
		Button szukajOK= new Button(shell, SWT.PUSH);
		szukajOK.setText("OK");
		
		table.setLinesVisible(true);
		table.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				String string = "";
		        selection = table.getSelection();
		      //  selectionP.add(table.getSelection()[0]);
		    //    for (int i = 0; i < selection.length; i++)
		         // string += selection[i] + " ";
		        
			}
		});
		table.setHeaderVisible(true);
		col1=new TableColumn(table, SWT.NONE);
		col1.setText("id");
		col2=new TableColumn(table, SWT.NONE);
		col2.setText("ilosc");
		Button wyswietlWszystkie = new Button(shell, SWT.PUSH);
		wyswietlWszystkie.setText("wyswietl wszystkei");
		wyswietlWszystkie.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				try{
					Connection dbConnection = Connector.getConnection();
					Statement statement = dbConnection.createStatement();
					statement.executeQuery("select id, nazwa, cena from produkty");
					ResultSet rs = statement.getResultSet();
					int count = 0;
					table.removeAll();
					while(rs.next())
					{
						int idVal=rs.getInt("id");
						String nazwaVal=rs.getString("nazwa");
						int cenaVal=rs.getInt("cena");
						final TableItem itemT = new TableItem(table, SWT.NONE);
						itemT.setText(new String[]{String.valueOf(idVal), nazwaVal,String.valueOf(cenaVal)});
					
						count++;
						shell.layout(true);
					}


				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
				col1.pack();
				col2.pack();
			}
		});
		Button addButton = new Button(shell, SWT.PUSH);
		addButton.setText("dodaj do oferty");
		addButton.addSelectionListener(new SelectionAdapter()
		{
			@Override 
			public void widgetSelected(SelectionEvent e)
			{
				selectionP.add(table.getSelection()[0]);
			}
		});
	
		
		shell.open();
		
	}

}
